/**
 * (C) copyright geolabs 2006
 * author: Gérald Fenoy
 */

/**
 * XmlToJson converter class
 */
System.require("HTMLEntity");
System.require("OctoXml");

XmlToJson=Class.extend({
  createTreeElements: function(){
	this.createTreeFromNode(arguments[0]);
  },

  createTreeFromNode: function(){
	var tree={};

	var me=arguments[0];
	var tempName=(arguments[0].init) ? arguments[0].init: me.root.tagName;
	//alert("Root => "+tempName+" ");

	tree[tempName]={};
	var tmp1="";
	
	var i=0;
	for(i=0;me.root.attributes.length;i++)
	  alert(i+'=>'+me.root.attributes[i]);
	for(i=0;i<me.root.childNodes.length;i++){
	  if(me.root.childNodes[i].nodeType==1 && 
	     me.root.childNodes[i].tagName!=me.loop){

	    var tmp=me.root.childNodes[i];
	    if(tmp.attributes.length==0){
	      tree[tempName][tmp.tagName]=-1;
	      if((tmp.childNodes.length>0)){
		tmp1+=tmp.tagName+" => "+tmp.childNodes.length+" ";
		if(tmp.childNodes.length>1){
		  var tmpX=tmp;
		  tmp1+="(multiple) \n* ";
		  tmp1+=tempName+" ok dumping => "+tmpX.tagName+" *";
		  tree[tempName][tmpX.tagName]=this.createTreeFromNode({root: tmpX,loop:null,init: tmpX.tagName})[tmpX.tagName];
		  tmp1+="\n 1*";
		  delete(tmpX);
		}
		else{
		  tree[tempName][tmp.tagName]=""+tmp.firstChild.nodeValue+"";
		}
	      }
	      else
		tree[tempName][tmp.tagName]=-1;
	    }
	    else{
	      var ltmp={};
	      for(j=0;j<tmp.attributes.length;j++){
		tmp1+=tempName+" ok dumping => "+tmp.attributes[j].name;
		ltmp[tmp.attributes[j].name]=""+tmp.attributes[j].nodeValue+"";
	      }
	      ltmp["value"]=(tmp.childNodes.length>0)?""+tmp.firstChild.nodeValue+"":-1;
	      tree[tempName][tmp.tagName]=ltmp;
	    }
	    tmp1+="\n";
	  }
	}
	return tree;
      }

  });

XmlToJson.define({

  _init: function(){ 
      this.tree={};
      this.xml=null;
      this.root=null;
      this.loop=null;
      this.elements=new Array();
      this.prefix="";

      this._init.loadArgv(this);
      this.setElement();
    },

  setXml: function(xml){
      this.xml=xml;
    },

  setRoot: function(root){
      this.root=root;
    },

  setLoop: function(loop){
      this.loop=loop;
      this.setNbElement();
    },

  setElement: function(){
      if(this.loop && this.xml){
	this.elements=this.xml.getElementsByTagName(this.loop);
      }
      else
	alert("Error: no loop ("+this.loop+") or no xml ("+this.xml+") given.");
    },

  getNbElement: function(){
	return this.elements.length;
    },

  createTree: function(){
      var i=0;

      this.rootName=this.root;
      //this.root=this.xml.getElementsByTagName(this.root)[0];

      this.tree=OctoXml.jsonFromElement(this.xml.getElementsByTagName(this.root)[0]);
      //this.debug();
      //alert("ok ended");
      //this.tree=XmlToJson.createTreeFromNode({root: this.xml.getElementsByTagName(this.root)[0],loop: this.loop});


      //for(i=0;i<this.elements.length;i++){
	//alert(""+i+"/"+this.elements.length);
	//this.tree[this.root][this.loop+""+i]=
      //XmlToJson.createTreeFromNode({root: this.elements[i],loop:null,init:this.loop})[this.loop];
      //     }

    },
      
      parseObject: function(obj,n,isRoot){
      var tmp="";
      tmp+=""+this.prefix+n+": "+((obj.length)?"[":((typeof obj)=="object")?"{":"")+"";
      
      if(obj.length && (typeof obj!="string")){
	for(var i=0;i<obj.length;i++){
	  var obji=obj[i];
	  tmp+="[ {"+this.parseObject(obji,i)+"} ]";
	}
      }
      else 
	if((typeof obj)=="object"){
	  this.oldPrefix=this.prefix;
	  this.prefix+="";
	  
	  for(j in obj){
	    var obj1=obj[j];
	  tmp+=this.parseObject(obj1,j)+",";
	  }
	  tmp=tmp.substr(0,tmp.length-1);
	  tmp+=this.prefix+" }";
	}
	else
	  tmp+=""+obj;
      return tmp;
    },

  debug: function(){

      tmp=OctoXml.dumpAsJson(this.tree);
      document.body.appendChild(document.createTextNode(tmp));

    }

  });
